﻿#include "LineEdit_TurnTableRate.h"

#include <qregexp.h>
#include <qvalidator.h>

QValidator* validator_positionMode = Q_NULLPTR;
QValidator* validator_rateMode = Q_NULLPTR;

LineEdit_TurnTableRate::LineEdit_TurnTableRate(QWidget *parent)
  : QLineEdit(parent)
{
  QRegExp regExp("^(-?\\d+)(\\.\\d+)?$");
  QRegExp regExp2("^(\\d+)(\\.\\d+)?$");
  validator_positionMode = new QRegExpValidator(regExp2,this);
  validator_rateMode = new QRegExpValidator(regExp,this);
  connect(this, &LineEdit_TurnTableRate::editingFinished, this, &LineEdit_TurnTableRate::slot_rateLineEditFinished);
  turnMode(RateMode);
  slot_updatePlaceHolder();
}

LineEdit_TurnTableRate::~LineEdit_TurnTableRate()
{
}
float LineEdit_TurnTableRate::getRate()
{
  QString value = text();
  if (value.isEmpty())
    {
      return turnTableRateLowerLimit;
    }
  else
    {
      return value.toFloat();
    }
}
void LineEdit_TurnTableRate::setRate(float rate)
{
  setText(QString::number(rate,'f',accuracy));
  slot_rateLineEditFinished();
}
void LineEdit_TurnTableRate::turnMode(Mode mode)
{
  this->mode = mode;
  switch (mode)
    {
    case LineEdit_TurnTableRate::PositionMode:
      setValidator(validator_positionMode);
      break;
    case LineEdit_TurnTableRate::RateMode:
      setValidator(validator_rateMode);
      break;
    default:
      break;
    }
  slot_rateLineEditFinished();
  slot_updatePlaceHolder();
}
void LineEdit_TurnTableRate::slot_rateLineEditFinished()
{
  QString rate = text();
  if (rate.isEmpty())
    {
      setText(QString::number(turnTableRateLowerLimit));
      return;
    }
  float rate1 = rate.toFloat();
  if (rate1 > turnTableRateUpperLimit)
    {
      setText(QString::number(turnTableRateUpperLimit));
    }
  else
    {
      switch (mode)
        {
        case LineEdit_TurnTableRate::PositionMode:
          if (rate1 < turnTableRateLowerLimit)
            {
              setText(QString::number(turnTableRateLowerLimit));
            }
          break;
        case LineEdit_TurnTableRate::RateMode:
          if (rate1 < -turnTableRateUpperLimit)
            {
              setText(QString::number(-turnTableRateUpperLimit));
            }
          else if (rate1 > -turnTableRateLowerLimit && rate1 < turnTableRateLowerLimit)
            {
              if (rate1 > 0)
                {
                  setText(QString::number(turnTableRateLowerLimit));
                }
              else if (rate < 0)
                {
                  setText(QString::number(-turnTableRateLowerLimit));
                }
              else
                {
                  setText(QString::number(turnTableRateLowerLimit));
                }
            }
          break;
        default:
          break;
        }
    }
}
void LineEdit_TurnTableRate::slot_updatePlaceHolder()
{
  switch (mode)
    {
    case LineEdit_TurnTableRate::PositionMode:
      setPlaceholderText(QString::number(turnTableRateLowerLimit) + QStringLiteral("至") + QString::number(turnTableRateUpperLimit));
      break;
    case LineEdit_TurnTableRate::RateMode:
      setPlaceholderText(QString::number(-turnTableRateUpperLimit) + QStringLiteral("至") + QString::number(-turnTableRateLowerLimit) + QStringLiteral("或") + QString::number(turnTableRateLowerLimit) + QStringLiteral("至") + QString::number(turnTableRateUpperLimit));
      break;
    default:
      break;
    }
}

LineEdit_TurnTableRate::Mode LineEdit_TurnTableRate::currentMode()
{
  return mode;
}
float LineEdit_TurnTableRate::getTurnTableRateUpperLimit()
{
  return turnTableRateUpperLimit;
}
void LineEdit_TurnTableRate::setTurnTableRateUpperLimit(float turnTableRateUpperLimit)
{
  this->turnTableRateUpperLimit = turnTableRateUpperLimit;
  slot_updatePlaceHolder();
}
float LineEdit_TurnTableRate::getTurnTableRateLowerLimit()
{
  return turnTableRateLowerLimit;
}
void LineEdit_TurnTableRate::setTurnTableRateLowerLimit(float turnTableRateLowerLimit)
{
  this->turnTableRateLowerLimit = turnTableRateLowerLimit;
  slot_updatePlaceHolder();
}

int LineEdit_TurnTableRate::getAccuracy()
{
  return accuracy;
}
void LineEdit_TurnTableRate::setAccuracy(int accuracy)
{
  this->accuracy = accuracy;
}
